﻿/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
 *~*  17-Oct-2009
 *~*  www.Learning-Leisure.com
 *~*  Project : PB.FBMembership.DAL    Namespace : PB.FBMembership.DAL
 *~*  File    : DataReaderHelper.cs       Class  : DataReaderHelper
 *~*  Author : Pinal Bhatt 
 *~*            self@P-Bhatt.com, www.P-Bhatt.com
 *~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace PB.FBMembership.DAL
{
    public class DataReaderHelper
    {

        #region Public Methods

        /// <summary>
        /// Gets value from the specified DataReader for the specified Column name and
        /// throws exception if corresponding field name contains DBNull
        /// </summary>
        public static T GetData<T>(IDataReader dr, string columnName)
        {
            return GetData<T>(dr, columnName, default(T), true);
        }

        /// <summary>
        /// Gets value from the specified DataReader for the specified Column name with
        /// the option to throw exception or retun default value in case of DBNull
        /// </summary>
        public static T GetData<T>(IDataReader dr, string columnName, bool throwExpIfDBNull)
        {
            return GetData<T>(dr, columnName, default(T), throwExpIfDBNull);
        }

        /// <summary>
        /// Gets value from the specified DataReader for the specified Column name and
        /// retuns specified default value in case of DBNull
        /// </summary>
        public static T GetData<T>(IDataReader dr, string columnName, T valueInCaseOfDBNull)
        {
            return (T)GetData(dr, columnName, valueInCaseOfDBNull, false);
        }

        

        #endregion

        #region Private Methods
    private static T GetData<T>(IDataReader reader, string columnName, T valueInCaseOfDBNull, bool throwExpIfDBNull)
    {
        string errMsg = "Error from DataReaderHelper.GetData, Column Type : " + typeof(T).ToString() + " Additional Info : {0}.";
        try
        {
            if (reader != null && !string.IsNullOrEmpty(columnName))
            {
                int columnIndex = reader.GetOrdinal(columnName);
                T retValue;
                if (!reader.IsDBNull(columnIndex))
                {
                    retValue = (T)reader.GetValue(columnIndex);

                }
                else
                {
                    if (throwExpIfDBNull)
                    {
                        throw new DataException(string.Format(errMsg,  "DBNull found while reading " + columnName));
                    }
                    else
                    {
                        retValue = valueInCaseOfDBNull;
                    }
                }
                return retValue;
            }
            else
            {
                throw new DataException(string.Format(errMsg, "DataReader object is null or ColumnName is null or empty"));
            }
        }
        catch (Exception ex)
        {
            throw new DataException(string.Format(errMsg, "Error while reading column " + columnName), ex);
        }

    }

        #endregion


    }
}
